/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.server.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.aeky.domain.User;
import com.aeky.server.dao.IUserDao;

/**
 * 
 * @author aeky
 */
@Repository("userDao")
public class UserDaoImpl extends BaseDaoSupport<User> implements IUserDao {

    @Override
    public void updateSingleUserById(Long id) {
        Query query = this.getEntityManager().createNativeQuery("update t_user set user_name = 'aeky' where id = :id");
        query.setParameter("id", id);
        query.executeUpdate();
    }

    /*
     * 
     * @see com.aeky.server.dao.IUserDao#findUserByLoginName(java.lang.String)
     */
    @Override
    public User findUserByLoginName(String loginName) {
        Query query = this.getEntityManager().createQuery("from User u where u.loginName = :loginName");
        query.setParameter("loginName", loginName);
        List<User> userList = (List<User>) query.getResultList();
        if (userList != null && userList.size() == 1) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public List<User> findAllUser() {
        Query query = this.getEntityManager().createQuery(" from User ");
        return query.getResultList();
    }

}
